#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory multiple times
#  in // over multiple volumes, and run a restore for each Job.
#
TestName="many-jobs-test"
JobName=SpanVol
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/build" >${cwd}/tmp/file-list

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Concurrent Jobs", "50", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Concurrent Jobs", "50", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Director")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Job")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SpoolData", "no", "Job")'

change_jobname CompressedTest $JobName
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
label storage=File volume=TestVolume002
label storage=File volume=TestVolume003
label storage=File volume=TestVolume004
label storage=File volume=TestVolume005
label storage=File volume=TestVolume006
update volume=TestVolume001 maxvolbytes=10M
update volume=TestVolume002 voluse=30s maxvolbytes=10M
update volume=TestVolume003 maxvoljobs=1 maxvolbytes=10M
update volume=TestVolume004 voluse=10s maxvolbytes=10M
update volume=TestVolume005 maxvoljobs=2 maxvolbytes=10M
setbandwidth limit="400 kb/s" client
run level=full job=$JobName yes
run level=full job=$JobName yes
run level=full job=$JobName yes
run level=full job=$JobName yes
run level=full job=$JobName yes
run level=full job=$JobName yes
run level=full job=$JobName yes
run level=full job=$JobName yes
run level=full job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bacula

# 
# now do several restores to ensure we cleanup between jobs
#

cat <<END_OF_DATA >$tmp/bconcmds.template
@$out $tmp/log-@jobid@-@nb@
restore where=$tmp/bacula-restores-@jobid@-@nb@ jobid=@jobid@ storage=File
cd @dirname@
m @basename@
done
yes
quit
END_OF_DATA

for jobid in `seq 1 8`
do
    nb=0
    for f in `find $cwd/build/src -type f`
    do
        nb=`expr $nb + 1`
        dirname=`dirname $f`
        basename=`basename $f`
        sed "s:@dirname@:$dirname:g" $tmp/bconcmds.template | \
            sed "s:@basename@:$basename:g" | \
            sed "s:@jobid@:$jobid:g" | \
            sed "s:@nb@:$nb:g" > $tmp/bconcmds
        rm -rf $tmp/bacula-restores-${jobid}-${nb}
        run_bconsole
        echo "diff -q $f $tmp/bacula-restores-${jobid}-${nb}/$f || exit 1" >> $tmp/totest
    done
done

echo wait > $tmp/cmd
run_bconsole $tmp/cmd

chmod +x $tmp/totest
$tmp/totest

if [ $? -ne 0 ]; then
    print_debug "ERROR: found differences after restore"
    estat=1
fi

check_for_zombie_jobs storage=File
stop_bacula

cat $tmp/log-* > $tmp/log2.out

check_two_logs
check_restore_diff
end_test
